package ch.ethz.fcl.metrobuzz.render.techniques.colors;

import java.util.HashMap;
import java.util.Map;

import org.lwjgl.opengl.GL11;

public class MBColorTable {
	public static final int WHITE = 1100;
	public static final int BLACK = 1101;
	public static final int GREY = 1102;

	public static final int RED = 0;
	public static final int CYAN = 4;
	public static final int YELLOW = 2;
	public static final int BLUE = 6;
	public static final int FUCHSIA = 8;
	public static final int LIME = 3;
	public static final int PURPLE = 7;
	public static final int ORANGE = 1;
	public static final int GREEN = 5;
	public static final int BROWN = 9;

	public static final int LIGHTGREY = 321;
	public static final int LIGHTBLUE = 322;

	public static final int COLORS = 9;

	public static Map<Integer, MBColor> colorMap = null;

	private static void init() {
		colorMap = new HashMap<Integer, MBColor>();
		MBColor color = new MBColor(0, 1.0f, 1.0f);

		int gap = 30;
		for (int i = 0; i <= COLORS; i++) {
			int h = gap * i;
			if (i > 3)
				h = gap * (i + 2);
			MBColor cc = color.transformHSV(h, 0.8f, 1.0f);
			colorMap.put(i, cc);
		}

		for (int i = 0; i <= COLORS; i++) {
			MBColor baseColor = colorMap.get(i);
			// float[] hsv = baseColor.getHSV();
			for (int j = 0; j < 10; j++) {
				float h = -10 + (j + 15) % 10 * 3;
				float s = 1.0f, v = 1.0f;
				if (j % 3 == 1) {
					// s = (10 - j) * 0.1f + 0.4f;
					// v = 0.7f + (10 - j) * 0.1f;
					s = 0.7f;
					v = 0.8f;
				} else if (j % 3 == 2) {
					s = 0.5f;
					v = 1.0f;
					// s = - (10 - j) * 0.05f + 0.4f;
					// v = 0.7f - (10 - j) * 0.05f;
				}
				MBColor cc = baseColor.transformHSV(h, s, v);
				colorMap.put((i + 1) * 10 + j, cc);
			}
		}

		MBColor lightgray = MBColorFactory.create(215, 210, 211);
		colorMap.put(LIGHTGREY, lightgray);

		MBColor gray = MBColorFactory.create(59, 49, 49);
		colorMap.put(GREY, gray);

		MBColor blue = MBColorFactory.create(80, 160, 234);
		colorMap.put(LIGHTBLUE, blue);

		MBColor white = MBColorFactory.create(255, 255, 255);
		colorMap.put(WHITE, white);

		MBColor black = MBColorFactory.create(34, 32, 16);
		colorMap.put(BLACK, black);
	}

	public static void assignColor(int index, float alpha) {
		float[] color = getRGBColor(index, alpha);
		GL11.glColor4f(color[0], color[1], color[2], alpha);
	}

	// assign gradual color change from color1 to color2
	public static void assignGradualColor(int colorIndex1, int colorIndex2,
			float ratio, float alpha, int pow) {
		float[] color1 = getRGBColor(colorIndex1, alpha);
		float[] color2 = getRGBColor(colorIndex2, alpha);

		float changeRatio = 1;
		for (int i = 0; i < pow; i++)
			changeRatio *= ratio;

		float r = color1[0] + changeRatio * (color2[0] - color1[0]);
		float g = color1[1] + changeRatio * (color2[1] - color1[1]);
		float b = color1[2] + changeRatio * (color2[2] - color1[2]);

		GL11.glColor4f(r, g, b, alpha);
	}

	public static float[] getRGBColor(int index, float alpha) {
		if (colorMap == null)
			init();
		if (index == WHITE) {
			float[] color = { 1.0f, 1.0f, 1.0f, alpha };
			return color;
		} else if (index == BLACK) {
			float[] color = { 34.0f / 255, 32.0f / 255, 16.0f / 255, alpha };
			return color;
		}

		MBColor mbcolor = colorMap.get(index);
		float[] color = null;
		if (mbcolor != null)
			color = mbcolor.getRGB();
		else
			color = colorMap.get(BLACK).getRGB();

		float[] cc = { color[0], color[1], color[2], alpha };
		return cc;
	}
}
